//
//  TDForgetPwdViewController.m
//  TFB
//
//  Created by Nothing on 15/3/16.
//  Copyright (c) 2015年 TD. All rights reserved.
//

#import "TDForgetPwdViewController.h"
#import "TDLoginViewController.h"

@interface TDForgetPwdViewController ()
{
    UITextField *_valueTF;
    UITextField *_newPwdTF;
    UITextField *_againNewPwdTF;
}
@end

@implementation TDForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self creatUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)creatUI
{
    //
    UIImageView *oldPwdBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_input.png"]];
    oldPwdBg.frame = CGRectMake(8, 20, SVFS.width - 16, 44);
    oldPwdBg.userInteractionEnabled = YES;
    [self.view addSubview:oldPwdBg];
    
    UIImageView *phoneImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_mobile"]];
    phoneImage.frame = CGRectMake(8, 7, 26, 30);
    [oldPwdBg addSubview:phoneImage];
    
    UIView *carveUpLine = [[UIView alloc] init];
    carveUpLine.backgroundColor = [UIColor colorWithRed:28/255.0f green:130/255.0f blue:168/255.0f alpha:1.0f];
    carveUpLine.frame = CGRectMake(phoneImage.frame.origin.x + phoneImage.frame.size.width + 8, 5, 1, 34);
    [oldPwdBg addSubview:carveUpLine];
    
    _valueTF = [[UITextField alloc] init];
    _valueTF.frame = CGRectMake(carveUpLine.frame.origin.x + carveUpLine.frame.size.width + 8, 0, oldPwdBg.bounds.size.width - (carveUpLine.frame.origin.x + carveUpLine.frame.size.width + 8), oldPwdBg.bounds.size.height);
    _valueTF.secureTextEntry = YES;
    _valueTF.placeholder = @"请输入原始密码";
    [oldPwdBg addSubview:_valueTF];
    
    
    //
    UIImageView *newPwdBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_input.png"]];
    newPwdBg.frame = CGRectMake(8, 10 + oldPwdBg.frame.size.height + oldPwdBg.frame.origin.y, SVFS.width - 16, 44);
    newPwdBg.userInteractionEnabled = YES;
    [self.view addSubview:newPwdBg];
    
    UIImageView *pwdImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_pwd"]];
    pwdImage.frame = CGRectMake(8, 7, 26, 30);
    [newPwdBg addSubview:pwdImage];
    
    UIView *carveUpLinePwd = [[UIView alloc] init];
    carveUpLinePwd.backgroundColor = [UIColor colorWithRed:28/255.0f green:130/255.0f blue:168/255.0f alpha:1.0f];
    carveUpLinePwd.frame = CGRectMake(pwdImage.frame.origin.x + pwdImage.frame.size.width + 8, 5, 1, 34);
    [newPwdBg addSubview:carveUpLinePwd];
    
    _newPwdTF = [[UITextField alloc] init];
    _newPwdTF.frame = CGRectMake(carveUpLinePwd.frame.origin.x + carveUpLinePwd.frame.size.width + 8, 0, newPwdBg.bounds.size.width - (carveUpLinePwd.frame.origin.x + carveUpLinePwd.frame.size.width + 8), newPwdBg.bounds.size.height);
    _newPwdTF.secureTextEntry = YES;
    if (self.isNeedOldPwd == NO) {
        oldPwdBg.hidden = YES;
        newPwdBg.frame = CGRectMake(8, 20, SVFS.width - 16, 44);
    }
    _newPwdTF.placeholder = @"6-20个数字加字母";
    [newPwdBg addSubview:_newPwdTF];
    
    
    //
    UIImageView *againNewPwdBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_input.png"]];
    againNewPwdBg.frame = CGRectMake(8, 10 + newPwdBg.frame.size.height + newPwdBg.frame.origin.y, SVFS.width - 16, 44);
    againNewPwdBg.userInteractionEnabled = YES;
    [self.view addSubview:againNewPwdBg];
    
    UIImageView *againPwdImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_pwd"]];
    againPwdImage.frame = CGRectMake(8, 7, 26, 30);
    [againNewPwdBg addSubview:againPwdImage];
    
    UIView *againCarveUpLinePwd = [[UIView alloc] init];
    againCarveUpLinePwd.backgroundColor = [UIColor colorWithRed:28/255.0f green:130/255.0f blue:168/255.0f alpha:1.0f];
    againCarveUpLinePwd.frame = CGRectMake(againPwdImage.frame.origin.x + againPwdImage.frame.size.width + 8, 5, 1, 34);
    [againNewPwdBg addSubview:againCarveUpLinePwd];
    
    _againNewPwdTF = [[UITextField alloc] init];
    _againNewPwdTF.frame = CGRectMake(againCarveUpLinePwd.frame.origin.x + againCarveUpLinePwd.frame.size.width + 8, 0, againNewPwdBg.bounds.size.width - (againCarveUpLinePwd.frame.origin.x + againCarveUpLinePwd.frame.size.width + 8), againNewPwdBg.bounds.size.height);
    _againNewPwdTF.secureTextEntry = YES;
    _againNewPwdTF.placeholder = @"请再次输入密码";
    [againNewPwdBg addSubview:_againNewPwdTF];
    
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    commitBtn.frame = CGRectMake(8, againNewPwdBg.frame.origin.y + againNewPwdBg.frame.size.height + 30, SVFS.width - 16, 40);
    commitBtn.backgroundColor = [UIColor colorWithRed:63.0/255.0 green:157.0/255.0 blue:176.0/255.0 alpha:1];
    [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [commitBtn setTitle:@"确认" forState:UIControlStateNormal];
    [commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitBtn];
    
}

- (BOOL)checkInput
{
    if (_newPwdTF.text.length < 6 | _newPwdTF.text.length > 20) {
        [self.view makeToast:@"新密码格式不符合,请重新输入" duration:2.0f position:@"center"];
        return NO;
    }
    
    if (![_newPwdTF.text isEqualToString:_againNewPwdTF.text]) {
        [self.view makeToast:@"两次输入密码不一致,请重新输入" duration:2.0f position:@"center"];
        return NO;
    }
    
    return YES;
}

- (void)commitBtnClick
{
    [self.view endEditing:YES];
    
    if (![self checkInput]) {
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSString *pwdType = @"1";
    if (!self.isEditLoginPwd) {
        pwdType = @"2";
    }
    
    NSString *updataType = @"1";
    if (!self.isNeedOldPwd) {
        updataType = @"2";
    }
    
    if (!self.value) {
        self.value = _valueTF.text;
    }
    
    if (!self.custMobile) {
        self.custMobile = @"13273901442";
    }
    
    [TDHttpEngine requestForUpdatePwdWithPwdType:pwdType updateType:updataType value:!self.isNeedOldPwd ? self.value : [self.value md5] newPwd:[_newPwdTF.text md5] custMobile:self.custMobile complete:^(BOOL succeed, NSString *msg, NSString *cod) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if (succeed) {
            [self.view makeToast:msg duration:2.0f position:@"center"];
            [self performSelector:@selector(popToLogin) withObject:self afterDelay:2.0f];

        }
        else{
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
    
}

- (void)popToLogin
{
    TDLoginViewController *loginVC = [[TDLoginViewController alloc] init];
    UINavigationController *loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [loginNav.navigationBar setBarStyle:UIBarStyleBlack];
    self.view.window.rootViewController = loginNav;
}

////密码修改测试
//- (void)xiugaimima
//{
//    [TDHttpEngine requestForUpdatePwdWithPwdType:@"1" updateType:@"1" value:@"111111" newPwd:@"222222" custMobile:@"13273901442" complete:^(BOOL succeed, NSString *msg, NSString *cod) {
//        
//    }];
//
//}
//
//- (void)zhaohuimima
//{
//    
//    [TDHttpEngine requestForUpdatePwdWithPwdType:@"1" updateType:@"2" value:@"000000" newPwd:@"111111" custMobile:@"13273901442" complete:^(BOOL succeed, NSString *msg, NSString *cod) {
//        
//    }];
//}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

//获取省份列表测试
- (void)testGetProv
{
    
    [TDHttpEngine requestForGetMsgCodeWithCustMobile:@"" codeType:@"" complete:^(BOOL succeed, NSString *msg, NSString *cod) {
        
    }];
}

@end
